var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var time = 0;
var touchDot = 0;//触摸时的原点
var interval = "";
Page({
  data: {
    navList: [],
    name:'',
    goodsList:[],
    id: 0,
    currentIndex:0,
    currentCategory: {},
    scrollLeft: 0,
    leftNavId:'',
    rightNavId:'',
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    size: 10000
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if (options.search) {
      that.setData({
        name: options.search
      });
    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

      this.getCategoryInfo();


  },
    onPullDownRefresh: function () {
        this.getCategoryInfo();
    },
  touchNavTo: function (id) {
      if (this.data.id == id) {
          return false;
      }
      this.setData({
          id: id
      });

      // this.getCategoryInfo();
  },
  // 触摸开始事件
  touchStart: function (e) {
      touchDot = e.touches[0].pageX; // 获取触摸时的原点
      // 使用js计时器记录时间
      interval = setInterval(function () {
          time++;
      }, 100);
  },
  // 触摸结束事件
  touchEnd: function (e) {
      var touchMove = e.changedTouches[0].pageX;
      // 向左滑动
      if (touchMove - touchDot <= -60 && time < 10) {
          //执行切换页面的方法
          if(this.data.rightNavId){
              this.touchNavTo(this.data.rightNavId)
          }

      }
      // 向右滑动
      if (touchMove - touchDot >= 60 && time < 10) {
          //执行切换页面的方法
          if(this.data.leftNavId){

              this.touchNavTo(this.data.leftNavId)

          }

      }
      clearInterval(interval); // 清除setInterval
      time = 0;
  },
  getCategoryInfo: function () {
    let that = this;
    util.request(api.FindCommodityByChangeAttribute, { name: this.data.name },'POST')
      .then(function (res) {
        console.log(123,res)
        if (res.code == 200) {
          that.setData({
            goodsList: res.result
          });

        }
        
      });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  switchCate: function (event) {
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id
    });

    // this.getCategoryInfo();
  }
})